package cc.bluemc.modules.generateCode.service.impl;

import cc.bluemc.modules.generateCode.entity.ColumnBean;
import cc.bluemc.modules.generateCode.entity.TableBean;
import cc.bluemc.modules.generateCode.service.IGenerateTableService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GenerateTableServiceImpl_MySQL extends IGenerateTableService{
	private Log log = LogFactory.getLog(this.getClass());
	@Override
	public String createSql(TableBean table) {
		StringBuffer sqlBuffer = new StringBuffer("DROP TABLE IF EXISTS ").append(table.getTableName()).append(';');
		sqlBuffer.append(" CREATE TABLE ").append(table.getTableName()).append('(');
		String comment;
		for(ColumnBean column:table.getColumnBeanList()){
			sqlBuffer.append(column.getColumnName()).append(" ");
			sqlBuffer.append(column.getColumnType()).append(" ");
			if(column.isPriKey()){
				sqlBuffer.append("NOT NULL AUTO_INCREMENT ");
			}
			comment=column.getColumnComment();
			if(StringUtils.isNotBlank(comment)){
				sqlBuffer.append("COMMENT '").append(comment).append("'");
			}
			sqlBuffer.append(" ,");
		}
		sqlBuffer.append("PRIMARY KEY (").append(table.getPriKey()).append(')');
		sqlBuffer.append(") ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; ");
		return sqlBuffer.toString();
	}
	@Override
	public String createSql(List<TableBean> tables) {
		StringBuffer sqlBuffer = new StringBuffer();
		for(TableBean tb:tables){
			sqlBuffer.append(createSql(tb));
		}
		return sqlBuffer.toString();
	}

}
